package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.SendGlobalImMessageBean;
import com.ymatou.iapi.livebuyer.service.SendGlobalImMessageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
*发送群组消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendGlobalImMessage{
	private static SendGlobalImMessageBean SendGlobalImMessageBean;
	private static SendGlobalImMessageCall SendGlobalImMessageCall;
	//?这个id需要客户端生成
	private static String groupid="@TGS#aGVENU5EP";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送群组消息");
	}

	@Before
	public void caseUp() {
		SendGlobalImMessageBean=new SendGlobalImMessageBean();
		SendGlobalImMessageCall=new SendGlobalImMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(SendGlobalImMessageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	@org.junit.Ignore
	public void Tc_SendGlobalImMessage_001() {
		Logger.start(true,"发送消息成功");
		try {
			//现在没有有效用户 这个case 会failed
			String uid="110";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setUserId(uid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(SendGlobalImMessageBean.getMessage().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(1,filter.size(),"验证日志");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendGlobalImMessage_002() {
		Logger.start(true,"不输入userid失败");
		try {
			String uid="110";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
//			Logger.verifyIsContains("错误的请求参数", SendGlobalImMessageCall.getString("message"), "判断message");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(SendGlobalImMessageBean.getMessage().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(0,filter.size(),"验证未匹配日志");
		} catch (Exception e) {

		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendGlobalImMessage_003() {
		Logger.start(true,"不输入countent失败");
		try {
			String uid="110";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageBean.setUserId("123456");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(0,filter.size(),"验证未匹配日志");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendGlobalImMessage_004() {
		Logger.start(true,"不存在用户发送消息失败");
		try {
			String uid="-123456";
//			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
//			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			SendGlobalImMessageBean.setUserId(uid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(SendGlobalImMessageBean.getMessage().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(0,filter.size(),"验证未匹配日志");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendGlobalImMessage_005() {
		Logger.start(true,"用户不在线发送消息失败");
		try {
			String uid="123456";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 0);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageBean.setUserId(uid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
			
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("fail-")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(1,filter.size(),"验证not login");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendGlobalImMessage_006() {
		Logger.start(true,"用户没有在腾讯云登入过,发送消息失败");
		try {
			String uid="123456";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 1);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageBean.setUserId(uid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyIsContains("fail", SendGlobalImMessageCall.getReturnData().toLowerCase(), "判断返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.toLowerCase().contains("invalid")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证invalid uin");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendGlobalImMessage_007() {
		Logger.start(true,"用户断线发送消息失败");
		try {
			String uid="123456";
			LiveRoomCallService.ChargeGlobalIMOnlineType(uid, 2);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			SendGlobalImMessageBean.setRequestId(reqid);
			SendGlobalImMessageBean.setUserId(uid);
			SendGlobalImMessageBean.setMessage("test message");
			SendGlobalImMessageCall.setData(SendGlobalImMessageBean);
			SendGlobalImMessageCall.callService();			
			Logger.verifyEquals("ok", SendGlobalImMessageCall.getReturnData(), "判断返回");
			
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("fail")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(1,filter.size(),"验证not login");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}